package com.jt.www.common.controller;

import com.jt.www.common.service.MessageService;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.entity.MessageInfoEntity;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.model.vo.MessageNumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin_ltf
 * @create 2019-04-14 13:41
 */
@Api(value = "消息维护接口", tags = {"MESSAGE-INFO-API"}, description = "消息维护接口")
@RestController
@RequestMapping(value = "/messageinfo")
public class MessageInfoController {

    public static final Logger log = LoggerFactory.getLogger(MessageInfoController.class);
    @Autowired
    private MessageService messageService;

    /**
     * 增加消息
     *
     * @param messageInfoEntity
     * @return
     */
    @ApiOperation(value = "增加消息", notes = "增加消息", httpMethod = "POST")
    @PostMapping(value = "/addMessageInfo")
    public ResultDTO insertMessageInfo(@RequestBody MessageInfoEntity messageInfoEntity) {

        Integer num = messageService.addMessageInfo(messageInfoEntity);
        if (num == 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "添加成功");
        }
        return new ResultDTO(ResultEnum.SERVER_ERROR, "添加失败");
    }


    /**
     * 根据目标用户查询消息
     */
    @ApiOperation(value = "根据目标用户查询消息", notes = "根据目标用户查询消息", httpMethod = "GET")
    @GetMapping(value = "/getMessageByTargetUser")
    public ResultDTO searchMessageByTargetUser(@ApiParam(value = "目标用户", required = true) @RequestParam("targetUser") String targetUser,
                                               @ApiParam(value = "分页参数，第几页", required = true) @RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @ApiParam(value = "分页参数，每页显示条数", required = true) @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        if (StringUtils.isEmpty(targetUser)) {
            log.error("【根据目标用户查询消息】targetUser为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        HashMap<String, Object> resultMap = messageService.searchMessageByTargetUser(targetUser, page, rows);
        return new ResultDTO(ResultEnum.SUCCESS, resultMap);
    }


    /**
     * 根据目标机构查询消息
     */
    @ApiOperation(value = "根据目标机构查询消息", notes = "根据目标机构查询消息", httpMethod = "GET")
    @GetMapping(value = "/getMessageByTargetOrganization")
    public ResultDTO searchMessageByTargetOrganization(@ApiParam(value = "目标机构", required = true) @RequestParam("targetOrganization") String targetOrganization,
                                                       @ApiParam(value = "分页参数，第几页", required = true) @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @ApiParam(value = "分页参数，每页显示条数", required = true) @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        if (StringUtils.isEmpty(targetOrganization)) {
            log.error("【根据目标机构查询消息】targetOrganization为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        HashMap<String, Object> messageInfoEntities = messageService.searchMessageByTargetOrganization(targetOrganization, page, rows);
        return new ResultDTO(ResultEnum.SUCCESS, messageInfoEntities);
    }

    /**
     * 根据目标地区查询消息
     */
    @ApiOperation(value = "根据目标地区查询消息", notes = "根据目标地区查询消息", httpMethod = "GET")
    @GetMapping(value = "/getMessageByTargetArea")
    public ResultDTO searchMessageByTargetArea(@ApiParam(value = "目标地区", required = true) @RequestParam("targetArea") String targetArea,
                                               @ApiParam(value = "分页参数，第几页", required = true) @RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @ApiParam(value = "分页参数，每页显示条数", required = true) @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        if (StringUtils.isEmpty(targetArea)) {
            log.error("【根据目标地区查询消息】targetArea为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        HashMap<String, Object> messageInfoEntities = messageService.searchMessageByTargetArea(targetArea, page, rows);
        return new ResultDTO(ResultEnum.SUCCESS, messageInfoEntities);
    }

    /**
     * 根据主键查询消息
     */
    @ApiOperation(value = "根据主键查询消息", notes = "根据主键查询消息", httpMethod = "GET")
    @GetMapping(value = "/getMessageById")
    public ResultDTO searchMessageById(@ApiParam(value = "消息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【根据id查询消息】id为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        MessageInfoEntity messageInfoEntities = messageService.searchMessageById(Integer.valueOf(id));
        return new ResultDTO(ResultEnum.SUCCESS, messageInfoEntities);
    }

    /**
     * 查询目标用户最新的一条未读（未通知）消息和未读消息的总数
     */
    @ApiOperation(value = "查询目标用户最新的一条未读（未通知）消息和未读消息的总数", notes = "查询目标用户最新的一条未读（未通知）消息和未读消息的总数", httpMethod = "GET")
    @GetMapping(value = "/getLatestUnreadMessageAndTotal")
    public ResultDTO searchLatestUnreadMessage(@ApiParam(value = "目标用户", required = true) @RequestParam("targetUser") String targetUser) {

        if (StringUtils.isEmpty(targetUser)) {
            log.error("【查询目标用户最新的一条未读（未通知）消息和未读消息的总数】targetUser为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        Map map = new HashMap<String, Object>();
        MessageInfoEntity messageInfoEntities = messageService.searchLatestUnreadMessage(targetUser);
        Integer totalNum = messageService.searchUnreadMessagetotalNumByUser(targetUser);
        map.put("messageInfoEntities", messageInfoEntities);
        map.put("totalNum", totalNum);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

//    /**
//     * 根据用户id，查询某一个用户所有未读（未通知）消息
//     */
//    @ApiOperation(value = "根据用户id，查询某一个用户所有未读（未通知）消息",notes = "根据用户id，查询某一个用户所有未读（未通知）消息",httpMethod = "GET")
//    @GetMapping(value = "/getUnreadMessageByUser")
//    public ResultDTO searchUnreadMessageByUser(@ApiParam(value = "目标用户", required = true) @RequestParam("targetUser") String targetUser,
//                                               @ApiParam(value = "分页参数，第几页", required = true) @RequestParam(value="page",defaultValue = "1") Integer page,
//                                               @ApiParam(value = "分页参数，每页显示条数", required = true) @RequestParam(value = "rows",defaultValue = "10") Integer rows){
//
//        if (StringUtils.isEmpty(targetUser)) {
//            log.error("【查询用户所有未读消息】targetArea为空");
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//
//        HashMap<String, Object> messageInfoEntities = messageService.searchUnreadMessageByUser(targetUser,page,rows);
//        return new ResultDTO(ResultEnum.SUCCESS, messageInfoEntities);
//    }
    /**
     * 根据目标用户，查询所有未读（未通知）消息的总数
     *    接口被合并  所以注释
     * @param targetUser
     * @return
     */
//    @ApiOperation(value = "根据目标用户，查询所有未读（未通知）消息的总数",notes = "据目标用户，查询所有未读（未通知）消息的总数",httpMethod = "GET")
//    @GetMapping(value = "/getUnreadTotalNumByUser")
//    public ResultDTO searchUnreadMessageByUser(@ApiParam(value = "目标用户", required = true) @RequestParam("targetUser") String targetUser){
//
//        if (StringUtils.isEmpty(targetUser)) {
//            log.error("【根据目标用户，查询所有未读（未通知）消息的总数】targetUser为空");
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//
//        Integer totalNum = messageService.searchUnreadMessagetotalNumByUser(targetUser);
//        return new ResultDTO(ResultEnum.SUCCESS, totalNum);
//    }

    /**
     * 用户点开消息后 需要把消息改为已读（已通知）
     */
    @ApiOperation(value = "用户点开消息后 需要把消息改为已读（已通知）", notes = "用户点开消息后 需要把消息改为已读（已通知）", httpMethod = "GET")
    @GetMapping(value = "/updateNoticeState")
    public ResultDTO updateNoticeStatetById(@ApiParam(value = "消息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【修改消息通知状态】id为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        int num = messageService.updateNoticeStatetById(Integer.valueOf(id));
        if (num == 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "修改成功");
        }
        return new ResultDTO(ResultEnum.SERVER_ERROR, "修改失败");
    }

    /**
     * 用户删除消息
     */
    @ApiOperation(value = "用户删除消息", notes = "用户删除消息", httpMethod = "GET")
    @GetMapping(value = "/deleteMessage")
    public ResultDTO deleteMessageInfoById(@ApiParam(value = "消息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【删除消息】id为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        int num = messageService.deleteMessageInfoById(Integer.valueOf(id));
        if (num == 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "修改成功");
        }
        return new ResultDTO(ResultEnum.SERVER_ERROR, "修改失败");
    }

    /**
     * 待办事项
     * 首页展示，根据目标用户查询待办消息种类和总数
     */
    @ApiOperation(value = "待办事项", notes = "待办事项", httpMethod = "GET")
    @GetMapping(value = "/getUnreadMessageNum")
    public ResultDTO searchUnreadMessageNumByTargetUser(@ApiParam(value = "目标用户", required = true) @RequestParam("targetUser") String targetUser) {

        if (StringUtils.isEmpty(targetUser)) {
            log.error("【待办事项】targetUser为空");
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }

        List<MessageNumVo> resultList = messageService.searchToDoItemByTargetUser(targetUser);
        return new ResultDTO(ResultEnum.SUCCESS, resultList);
    }

}
